package com.itluma.openapi.exception;

import com.itluma.openapi.bean.AjaxMessage;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class GlobalException {

    @ResponseBody
    @ExceptionHandler(OpenApiException.class)//该方法处理OpenApiException类型的异常
    public AjaxMessage handler(OpenApiException ex) {
        ExceptionEnum exceptionEnum = ex.getExceptionEnum();
        AjaxMessage ajaxMessage = new AjaxMessage();
        ajaxMessage.setCode(exceptionEnum.getCode());
        ajaxMessage.setMessage(exceptionEnum.getMessage());
        return ajaxMessage;
    }

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)//IOException
    public AjaxMessage handler2(RuntimeException ex) {
        ex.printStackTrace();;
        AjaxMessage ajaxMessage = new AjaxMessage();
        ajaxMessage.setCode(99999);
        ajaxMessage.setMessage(ex.getMessage());
        return ajaxMessage;
    }
}
